/***
 * * PROJECT
 * *    ATaG
 * * VERSION
 * *    $Revision: 1.2 $
 * * DATE
 * *    $Date: 2007/06/13 00:43:31 $
 * * LOG
 * *    $Log: RunnableTask.java,v $
 * *    Revision 1.2  2007/06/13 00:43:31  animesh
 * *    no message
 * *
 * *    Revision 1.4  2006/10/26 23:58:31  lmottola
 * *    Bug fixes: Hello World application running in SWANS.
 * *
 * *    Revision 1.3  2006/10/26 01:02:56  lmottola
 * *    Added methods to fire up simulation.
 * *
 * *    Revision 1.2  2006/10/25 19:36:38  animesh
 * *    Added this task to represent a running ATaG task.
 * *
 * *    Revision 1.1  2006/10/24 22:36:07  animesh
 * *    Changed functions for sending out data. Used a lot of old code, but Luca will have to integrate it.
 * *
 * *
 * *
 * *   ATaG (Abstract Task Graph) - Data-driven Macroprogramming for
 * *                                Sensor Networks
 * *
 * *   This program is free software; you can redistribute it and/or
 * *   modify it under the terms of the GNU General Public License
 * *   as published by the Free Software Foundation; either version 2
 * *   of the License, or (at your option) any later version.
 * *
 * *   This program is distributed in the hope that it will be useful,
 * *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 * *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * *   GNU General Public License for more details.
 * *
 * *   You should have received a copy of the GNU General Public License
 * *   along with this program; if not, you may find a copy at the FSF web
 * *   site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
 * *   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * *   Boston, MA  02111-1307, USA
 ***/

package atag.runtime;

import atag.common.ATaGTaskDeclaration;

/**
 * This Class is a "running ATaG Task". It stores a running task, but can also
 * be "run".
 * 
 * @author Animesh
 */
public class RunnableTask {

	/** This is the task declaration */
	private ATaGTaskDeclaration taskDecl;

	/** The runnable object that contains the task code */
	private Runnable taskCode;

	/** The priority of the thread */
	private int taskPriority;

	/** record if a runTask has been called on this or not. */
	private boolean hasBeenRun = false;

	/** Creates a new instance of RunnableTask */
	public RunnableTask(ATaGTaskDeclaration newTaskDecl, Runnable newTaskCode,
			int newTaskPriority) {

		this.taskCode = newTaskCode;
		this.taskDecl = newTaskDecl;
		this.taskPriority = newTaskPriority;
	}

	/**
	 * @returns the declaration of the associated task
	 */
	public ATaGTaskDeclaration getTaskDeclaration() {
		return this.taskDecl;
	}

	public void runTask() {
		// ERROR the following assumes that a periodic task cannot be anydata...
		if ((this.taskDecl.getFiringRule()[1] == ATaGTaskDeclaration.FIRING_PERIODIC)
				&& this.hasBeenRun) {
			System.err.println("Task is periodic, it is already running");
		} else {
			hasBeenRun = true;
			taskCode.run();
		}
	}

	public boolean stopTask() {
		// TODO: To be implemented
		return false;
	}

}
